{layout name="layout1" /}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
                <div class="layui-colla-item">
                    <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
                    <div class="layui-colla-content layui-show">
                        <p>*发布商品时可选统一规格或者多规格，满足商品不同销售属性设置。</p>
                        <p>*商品售卖状态分为销售中、仓库中、回收站。</p>
                        <p>*库存预警商品为库存不足的销售中、仓库中商品。</p>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-tab layui-tab-card" lay-filter="tab-all">


            <div class="layui-tab-item layui-show">
                <div class="layui-card">
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                        <div class="layui-form-item type">
                            <div class="layui-inline">
                                <label class="layui-form-label">商品名称:</label>
                                <div class="layui-input-block">
                                    <input type="text" name="keyword" id="keyword" placeholder="请输入关键词"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商品编码:</label>
                                <div class="layui-input-block">
                                    <input type="text" name="code" id="code" placeholder="请输入编码" autocomplete="off"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商品分类:</label>
                                <div class="layui-input-block">
                                    <select name="category_id" placeholder="请选择商品分类">
                                        <option value="0">全部</option>
                                        {foreach $category_list as $val }
                                        <option value="{$val.id}">{$val.html}{$val.name}</option>
                                        {/foreach}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">商品供货商:</label>
                                <div class="layui-input-block">
                                    <select name="supplier_id" id="supplier_id" placeholder="请选择商品供货商">
                                        <option value="0">全部</option>
                                        {foreach $supplier_list as $val }
                                        <option value="{$val.id}">{$val.name}</option>
                                        {/foreach}
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn layui-btn-sm layuiadmin-btn-goods {$view_theme_color}"
                                        lay-submit lay-filter="goods-search">查询
                                </button>
                                <button class="layui-btn layui-btn-sm layuiadmin-btn-goods layui-btn-primary "
                                        lay-submit lay-filter="goods-clear-search">清空查询
                                </button>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <div style="padding-bottom: 10px;" class="add">
                            <button class="layui-btn layui-btn-sm layuiadmin-btn-goods {$view_theme_color}"
                                    data-type="add">发布商品
                            </button>
                            <button id="lower" class="layui-btn layui-btn-sm layuiadmin-btn-goods {$view_theme_color}"
                                    data-type="lower">下架
                            </button>
                            <button id="upper" class="layui-btn layui-btn-sm layuiadmin-btn-goods {$view_theme_color}"
                                    data-type="upper" style="display: none;">上架
                            </button>
                        </div>
                        <table id="goods-lists" lay-filter="goods-lists"></table>
                        <script type="text/html" id="ratio">
                            <p>一级佣金：{{d.first_ratio_desc}}</p>
                            <p>二级佣金：{{d.second_ratio_desc}}</p>
                            <p>二级佣金：{{d.three_ratio_desc}}</p>
                            <!--                            <p >股东分红比例：{{d.region_ratio_desc}}</p>-->
                            <!--                            <p >区域分红比例：{{d.shareholder_ratio_desc}}</p>-->
                        </script>
                        <script type="text/html" id="new">
                            <input type="checkbox" lay-filter="switch-status" data-id={{d.id}} data-field='is_new'
                                   lay-skin="switch"
                                   lay-text="是|否" {{# if(d.is_new){ }} checked {{# } }}/>
                        </script>
                        <script type="text/html" id="best">
                            <input type="checkbox" lay-filter="switch-status" data-id={{d.id}} data-field='is_best'
                                   lay-skin="switch"
                                   lay-text="是|否" {{# if(d.is_best){ }} checked {{# } }}/>
                        </script>
                        <script type="text/html" id="like">
                            <input type="checkbox" lay-filter="switch-status" data-id={{d.id}} data-field='is_like'
                                   lay-skin="switch"
                                   lay-text="是|否" {{# if(d.is_like){ }} checked {{# } }}/>
                        </script>
                        <script type="text/html" id="goods-info">
                            <img src="{{d.image}}" style="height:60px;width: 60px;margin-right: 5px;"
                                 class="image-show"> {{d.name}}
                        </script>
                        <script type="text/html" id="goods-operation">
                            <a class="layui-btn layui-btn-sm" lay-event="edit">查看</a>
                            <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="status" vdata='2'>上架</a>
                            <a class="layui-btn layui-btn-normal layui-btn-sm operation-btn" lay-event="remarks">未通过</a>
                            <a class="layui-btn layui-btn-warm layui-btn-sm" lay-event="status" vdata='4'>作废</a>
                        </script>


                    </div>
                </div>
            </div>

        </div>
    </div>
</div>
<style>
    .layui-table-cell {
        height: auto;
    }
</style>
<script>
    layui.config({
        version: "{$front_version}",
        base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'like', 'form'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , like = layui.like
            , element = layui.element;

        //监听搜索
        form.on('submit(goods-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('goods-lists', {
                where: field
            });
        });
        //清空查询
        form.on('submit(goods-clear-search)', function () {
            $('#keyword').val('');  //清空输入框
            $('#code').val('');  //清空输入框
            $('#category_id').val(0);  //清空输入框
            $('#supplier_id').val(0);  //清空输入框
            form.render('select');
            //刷新列表
            table.reload('goods-lists', {
                where: []
            });
        });

        $('.layui-btn.layuiadmin-btn-goods').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //事件
        var active = {
            //添加商品
            add: function () {
                var index = layer.open({
                    type: 2
                    , title: '添加商品'
                    , content: '{:url("goods/add")}'
                    , area: ['90%', '90%']
                    , btn: ['保存', '取消']
                    , maxmin: true
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'goods-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field;
                            like.ajax({
                                url: '{:url("goods/add")}',
                                data: field,
                                type: "post",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        }, function () {
                                            window.location.href = window.location.href;
                                        });

                                    }
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                    , cancel: function (index, layero) {
                        var window_index = index;
                        layer.confirm('商品未保存，确定关闭吗？', {
                            time: 0, //不自动关闭
                            btn: ['确定', '取消'],
                            yes: function (index) {
                                layer.close(index);
                                layer.close(window_index);
                            }
                        });
                        return false;
                    }
                });
            },
            //批量下架
            lower: function () {
                var checkStatus = table.checkStatus('goods-lists');
                var checkData = checkStatus.data;
                var ids = [];
                // 取出选中的行ID
                checkData.forEach(function (item) {
                    ids.push(parseInt(item['id']));
                });
                if (ids.length <= 0) {
                    layui.layer.msg('未选择', {time: 1000});
                    return false;
                }
                // 提交数据
                like.ajax({
                    url: '{:url("goods/lowerStatus")}',
                    data: {"ids": ids},
                    type: "post",
                    success: function (res) {
                        if (res.code === 1) {
                            layui.layer.msg(res.msg, {offset: '15px', icon: 1, time: 1000});
                            // updateTabNumber();
                            table.reload('goods-lists', {where: []});
                        } else {
                            layui.layer.msg(res.msg, {offset: '15px', icon: 2, time: 1000});
                        }
                    }
                });

            },
            //批量上架
            upper: function () {
                var checkStatus = table.checkStatus('goods-lists');
                var checkData = checkStatus.data;
                var ids = [];
                // 取出选中的行ID
                checkData.forEach(function (item) {
                    ids.push(parseInt(item['id']));
                });
                if (ids.length <= 0) {
                    layui.layer.msg('未选择', {time: 1000});
                    return false;
                }
                // 提交数据
                like.ajax({
                    url: '{:url("goods/upperStatus")}',
                    data: {"ids": ids},
                    type: "post",
                    success: function (res) {
                        if (res.code === 1) {
                            layui.layer.msg(res.msg, {offset: '15px', icon: 1, time: 1000});
                            // updateTabNumber();
                            table.reload('goods-lists', {where: []});
                        } else {
                            layui.layer.msg(res.msg, {offset: '15px', icon: 2, time: 1000});
                        }
                    }
                });

            }
        };


        //获取列表
        getList(1);
        //切换列表
        element.on('tab(tab-all)', function (data) {
            var type = $(this).attr('data-type');
            getList(type);
            if (type === '1') {
                $("#lower").show();
                $("#upper").hide();
            } else if (type === '2') {
                $("#lower").hide();
                $("#upper").show();
            } else {
                $("#lower").hide();
                $("#upper").hide();
            }
        });

        //监听工具条
        table.on('tool(goods-lists)', function (obj) {
            var id = obj.data.id;
            var name = obj.data.name;
            //上下架
            if (obj.event === 'status') {
                var id = obj.data.id;
                var sta = this.getAttribute('vdata');
                var i = '';
                if (sta == "2") {
                    i = '确定商品上架:';

                } else if (sta == '4') {
                    i = '确定禁用供货商:';
                }
                layer.confirm(i + '<span style="color: red">' + obj.data.name + '</span>', function (index) {
                    like.ajax({
                        url: '{:url("goods/editStatus")}',
                        data: {goods_id: id, status: sta},
                        type: "post",
                        success: function (res) {
                            if (res.code == 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                // updateTabNumber();
                                table.reload('goods-lists'); //数据刷新
                            }
                        }
                    });
                });
                layer.close(index);
                // var tips = '确定上架当前商品：' + '<span style="color: red">' + name + '</span>';

                // var field_value = 1;

                // if (obj.data.status) {
                //     field_value = 0;
                //     tips = '确定下架当前商品：' + '<span style="color: red">' + name + '</span>';
                // }

                // layer.confirm(tips, function (index) {
                //     var fields = 'status';
                //     changeFields(id, fields, field_value);
                // })
            }
            if (obj.event === 'remarks') {
                var id = obj.data.id;
                var sta = this.getAttribute('vdata');
                layer.prompt({
                    title: '备注信息',
                    formType: 2,
                    value: ''
                }, function (value, index) {
                    layer.close(index);
                    like.ajax({
                        url: '{:url("goods/remarks")}'
                        , data: {'id': id, "goods_remarks": value, sta: sta}
                        , type: 'post'
                        , success: function (res) {
                            if (res.code === 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                            }
                        }
                    });

                });

            }
            //移入、移出回收站
            if (obj.event === 'recycle') {
                var tips = '确定移至回收站：' + '<span style="color: red">' + name + '</span>';
                var field_value = -1;
                if (obj.data.status == -1) {
                    field_value = 0;
                    tips = '确定放回仓库：' + '<span style="color: red">' + name + '</span>';
                }
                layer.confirm(tips, function (index) {
                    var fields = 'status';
                    changeFields(id, fields, field_value);
                })
            }
            if (obj.event === 'edit') {
                var id = obj.data.id;

                var index = layer.open({
                    type: 2
                    , title: '编辑商品'
                    , content: '{:url("goods/edit")}?goods_id=' + id
                    , area: ['90%', '90%']
                    , maxmin: true
                    , cancel: function (index, layero) {
                        // var window_index = index;
                        // layer.confirm('商品未保存，确定关闭吗？', {
                        //     time: 0, //不自动关闭
                        //     btn: ['确定', '取消'],
                        //     yes: function (index) {
                        //         layer.close(index);
                        //         layer.close(window_index);
                        //     }
                        // });
                        // return false;
                        layer.close(index);
                        layer.close(window_index);
                    }
                });
            }
            if (obj.event === 'del') {
                var name = obj.data.name;
                layer.confirm('确定删除商品:' + '<span style="color: red">' + name + '</span>', function (index) {
                    like.ajax({
                        url: '{:url("goods/del")}',
                        data: {id: id},
                        type: "post",
                        success: function (res) {
                            if (res.code == 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                // updateTabNumber();
                                table.reload('goods-lists'); //数据刷新
                            }
                        }
                    });
                    layer.close(index);


                })

            }
            if (obj.event === 'tips') {
                layer.tips('数字越大，越靠前', $(this), {tips: [1, '#FF5722'], time: 1500});
            }


        });
        //精选推荐、猜你喜欢
        form.on('switch(switch-status)', function (obj) {
            var id = obj.elem.attributes['data-id'].nodeValue
            var fields = obj.elem.attributes['data-field'].nodeValue
            var status = 0;
            if (this.checked) {
                status = 1;
            }

            changeFields(id, fields, status);
        })
        //商品排序
        table.on('edit(goods-lists)', function (obj) {
            var id = obj.data.id;
            var fields = 'sort';
            var field_value = obj.value;

            if (isNaN(field_value)) {
                var old_value = $(this).prev().text();

                layer.tips('请输入数字', $(this), {tips: [1, '#FF5722']});
                $(this).val(old_value);

                return false;
            }

            changeFields(id, fields, field_value);

        });

        //图片放大
        $(document).on('click', '.image-show', function () {
            var src = $(this).attr('src');
            like.showImg(src, 600);
        });


        function getList(type) {
            layui.define(['table', 'form'], function (exports) {
                var $ = layui.$
                    , table = layui.table
                    , form = layui.form;

                var cols = [
                    {type: 'checkbox'}
                    , {field: 'name', title: '商品名称', width: 250}
                    , {field: 'gb_name', width: 160, title: "分类"}
                    , {
                        field: 'image', width: 180, title: '图片', templet: function (value, row, index) {
                            var i = "";
                            if (value.image.length > 0) {
                                i = "<img class='image-show' src='" + value.image + "'>"
                            }
                            return i
                        }
                    }
                    , {field: 'remark', width: 100, title: '简介'}
                    , {
                        field: 'goods_links', width: 300, title: '商品链接', templet: function (value, row, index) {
                            var i = "";
                            if (value.goods_links.length > 0) {
                                i = "<span class='goods-link layui-btn layui-btn-xs layui-btn-normal'  data-link='" + value.goods_links + "'>" + value.goods_links + "</span>"
                            }
                            return i
                        }
                    }
                    , {
                        field: 'platform',width: 120,  title: '所属平台/状态', templet: function (value, row, index) {
                            var i = "";
                            var s = "";

                            if (value.platform == "抖音") {
                                i = '<img style="width: 20px;height: 20px"  src="/images/icon/抖音icon.png" alt="">';
                            } else if (value.platform == "快手") {
                                i = '<img style="width: 20px;height: 20px" src="/images/icon/快手icon.png" alt="">';
                            } else if (value.platform == "天猫") {
                                i = '<img style="width: 20px;height: 20px" src="/images/icon/天猫icon.png" alt="">';
                            }

                            if (value.status == "0") {
                                s = '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-disabled">下架</span>';
                            } else if (value.status == "1") {
                                s = '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-normal">上架审核</span>';
                            } else if (value.status == "2") {
                                s = '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn">正常</span>';
                            } else if (value.status == "3") {
                                s = '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-warm">未通过</span>';
                            } else if (value.status == "4") {
                                s = '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger">作废</span>';
                            }

                            return i +"&nbsp;/&nbsp;"+ s;
                        }
                    }
                    , {field: 'welfare', width: 160, title: '商品福利'}
                    , {field: 'commission', width: 160, title: '佣金比例'}
                    , {field: 'coupon', width: 80, title: '优惠券'}
                    , {field: 'good_price', width: 160, title: '商品最终价格'}
                    , {fixed: 'right', title: '操作', align: 'right', width: 300, toolbar: '#goods-operation'}
                ];

                //管理员管理
                table.render({
                    id: 'goods-lists'
                    , elem: '#goods-lists'
                    , url: '{:url("goods/lists")}?type=' + type  //模拟接口
                    , cols: [cols]
                    , page: true
                    , text: {none: '暂无数据！'}
                    , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                        return {
                            "code": res.code,
                            "msg": res.msg,
                            "count": res.data.count, //解析数据长度
                            "data": res.data.list, //解析数据列表
                        };
                    }
                    , done: function (res, curr, count) {
                        setTimeout(function () {
                            // 解决操作栏因为内容过多换行问题
                            $(".layui-table-fixed-l .layui-table-body").removeAttr("style");
                            $(".layui-table-fixed-r .layui-table-body").removeAttr("style");
                            $(".layui-table-main tr").each(function (index, val) {
                                console.log($(val).height());
                                $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                                $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                            });
                        }, 100)
                    }
                });

            });
        }

        $(document).on("click", ".goods-link", function () {
            var url = $(this).attr("data-link");
            window.open(url);
        });

        function changeFields(id, fields, value) {
            $.ajax({
                url: '{:url("goods/changeFields")}',
                data: {id: id, field: fields, value: value},
                type: 'post',
                dataType: 'json',
                success: function (res) {
                    if (res.code == 1) {
                        layui.layer.msg(res.msg, {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            if (fields == 'status') {
                                window.location.href = window.location.href;
                            }
                        });
                        // updateTabNumber();
                    } else {
                        layui.layer.msg(res.msg, {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                        });
                    }
                }
            })
        }

        /**
         * 更新选项卡 统计数据
         */
        function updateTabNumber() {
            like.ajax({
                url: '{:url("goods/totalCount")}',
                data: {},
                type: "GET",
                success: function (res) {
                    if (res.code === 1) {
                        $(".layui-tab-title li[data-type=1]").html("销售中商品(" + res.data.sell + ")");
                        $(".layui-tab-title li[data-type=2]").html("仓库中商品(" + res.data.warehouse + ")");
                        $(".layui-tab-title li[data-type=3]").html("库存预警商品(" + res.data.warn + ")");
                        $(".layui-tab-title li[data-type=4]").html("回收站商品(" + res.data.recycle + ")");
                    }
                }
            });
        }

    });
</script>